The data and some of the code were from the Urban Analytics workshop by Alex Singleton. The github link to this workshop can be found at Urban Analytics
library(tmap)
library(sf)
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
This data was created earlier in the lab, but we just load it here with load to keep things simpler and cleaner. I went through the whole lab, but I didn’t want to have all of Dr. Singleton’s code copied into my lab on my repository. I shortened it to just loading the clusters and mapping them.
load("./data/cluster_7.Rdata")
The cluster_7 object contains a list of different outputs related to the cluster analysis- we can view these:
str(cluster_7)
## List of 9
## $ cluster : Named int [1:1584] 7 5 7 5 5 7 5 1 1 4 ...
## ..- attr(*, "names")= chr [1:1584] "E00032987" "E00032988" "E00032989" "E00032990" ...
## $ centers : num [1:7, 1:60] 0.553 0.584 0.677 0.666 0.391 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:7] "1" "2" "3" "4" ...
## .. ..$ : chr [1:60] "k001" "k002" "k003" "k004" ...
## $ totss : num 2827
## $ withinss : num [1:7] 286 308 250 255 159 ...
## $ tot.withinss: num 1635
## $ betweenss : num 1192
## $ size : int [1:7] 259 340 279 334 109 73 190
## $ iter : int 6
## $ ifault : int 0
## - attr(*, "class")= chr "kmeans"
The cluster results can therefore be accessed as follows:
lookup <- data.frame(cluster_7$cluster)
lookup$OA <- rownames(lookup)
colnames(lookup) <- c("K_7","OA")
lookup$SUPER <- LETTERS[lookup$K_7]
We will also look at the distribution of these clusters:
table(lookup$K_7)
##
## 1 2 3 4 5 6 7
## 259 340 279 334 109 73 190
To get the boundaries, we use read_sf, which also works on .geojson files.
liverpool_sf = read_sf( "./data/Liverpool_OA_2011.geojson")
Here we merge the cluster data together with the boundary, by the OA code.
liverpool_sf <- merge(liverpool_sf, lookup, by.x="oa_code",by.y="OA")
To map the clusters, we use tmap. First we set tmap_mode("view"). Then we use tm_fill to map the cluster with palette Set2. The we add black borders with tm_borders. With tm_layout, we set the title to be “Cluster Map Liverpool”
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(liverpool_sf) +
tm_fill("SUPER", palette = "Set2") +
tm_borders(col = "black",alpha =.5) +
tm_layout(title = "Cluster Map Liverpool")
## Warning: The shape liverpool_sf is invalid. See sf::st_is_valid